package com.neepu.controller;

import com.neepu.pojo.User;
import com.neepu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RepasswordController {

    @Autowired
    private UserService userService;//接口实现类对象

    @RequestMapping("/repassword")
    public String repassword(){
        return "findPassword";
    }

    @RequestMapping(value = "/toFindPassword" )
    public ModelAndView toFindPassword(String loginname, String email, Integer role, ModelAndView mv){
        User user = userService.toFindPassword(loginname,email,role);
        if (user != null) {
            mv.setViewName("toUpdatePassword");
            mv.addObject("user",user);
            return mv;
        }
        mv.addObject("message","你输入的信息有误，请重新输入");
        mv.setViewName("forward:repassword");
        return mv;
    }

    @RequestMapping(value = "/updataPassword" )
    public ModelAndView toFindPassword(String loginname, String password, String repassword, ModelAndView mv){

        if (!password.equals(repassword)){
            mv.addObject("message","你两次输入的密码不一致");
            User user = new User();
            user.setUserID(loginname);
            mv.addObject("user",user);
            mv.setViewName("toUpdatePassword");
            return mv;
        }
        User user = new User();
        user.setUserID(loginname);
        user.setPassword(password);
        userService.updatePassword(user);
        mv.setViewName("forward:loginForm");
        return mv;
    }
}
